<template>
  <el-dialog
    :visible="isShow"
    :title="mode === 'add' ? '新增权限' : '编辑权限'"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="enVisible">
        <el-input v-model="form.enVisible" />
      </el-form-item>
      <el-form-item label="企业可见" prop="description">
        <el-switch
          v-model="form.description"
          active-text="不可见"
          inactive-text="可见"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="closeEvent">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission.js'
export default {
  name: 'Dialog',
  model: {
    prop: 'isShow'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mode: 'add',
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        enVisible: '', //	string	非必须
        description: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        enVisible: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  watch: {
    isShow(boolean) {
      console.log(boolean)
    }
  },

  methods: {
    closeEvent() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        enVisible: '', //	string	非必须
        description: '0', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await sysPermissionPost(this.form)
          } else {
            await sysPermissionPut(this.form)
          }
          this.$message.success(this.mode === 'add' ? '添加成功' : '修改成功')
          this.closeEvent()
          this.$emit('getData')
          //   console.log('成功')
        }
      })
    }
  }
}
</script>

<style></style>
